<!--vue模板-->
<template>
  <el-dialog
    @close="close"
    v-model="visible"
    :close-on-click-modal="false"
    :title="ruleForm.id ? '更新仓库' : '添加仓库'"
    width="60%"
    draggable
  >
    <!-- 表单数据 -->
    <el-form
      ref="ruleFormRef"
      :model="ruleForm"
      :rules="rules"
      label-width="120px"
      class="ruleForm"
      status-icon
    >
      <el-form-item label="仓库名称" prop="name">
        <el-input
          v-model="ruleForm.name"
          class="ipt"
          placeholder="请输入仓库名称"
        />
      </el-form-item>

      <!-- 选择树 -->
      <el-form-item label="所属类别" prop="categoryName">
        <el-tree-select
          v-model="ruleForm.categoryName"
          @node-click="handleNodeClick" 
          :data="treeData"
          check-strictly
          :render-after-expand="false"
          :props="defaultProps"
        />
      </el-form-item>

      <el-form-item label="仓库地址" prop="place">
        <el-input
          v-model="ruleForm.place"
          class="ipt"
          placeholder="请输入仓库地址"
        />
      </el-form-item>


      <el-form-item label="备注" prop="note">
        <el-input
          v-model="ruleForm.note"
          class="ipt"
          placeholder="请输入备注信息"
        />
      </el-form-item>

    
    </el-form>

    <template #footer>
      <span class="dialog-footer">
        <el-button @click="visible = false">取消</el-button>
        <el-button type="primary" @click="saveRep"> 确定 </el-button>
      </span>
    </template>
  </el-dialog>
</template>

<!--vue 组件选项定义-->
<script setup>
import { inject, onBeforeMount, reactive, ref } from "vue";
import { ElMessage,ElMessageBox } from "element-plus";
import host from "@/util/DEV_ADDRESS";
import { queryCategoryAll,  queryById } from "@/api/basic/Category.js";

import { getUserInfo } from '@/util/auth.js'

import { saveRepository,updateRepository } from '@/api/basic/Repository'






const ruleFormRef = ref(null);
// 控制添加更新的显示隐藏
const visible = inject("isShow");

// 定义树节点
const treeData = ref({})

async function initTree() {
  const res = await queryCategoryAll();
  treeData.value = res.data;
  
}

const defaultProps = {
  children: "children",
  label: "name",
  value: "name",
  id: "id",
};

onBeforeMount(() => {
  initTree()
})


const handleNodeClick = (data) => {
  ruleForm.value.categoryName = data.name
  ruleForm.value.categoryCode = data.code
}





// 表单
const ruleForm = ref({
  id: "",
  name: "",
  code: "",
  categoryName: "",
  categoryCode: "",
 note: '',
 place: '',
  adduserId: ""
});

function init(data){
  ruleForm.value = data
}
// 初始化表单数据
defineExpose({ init })

// 校验
const rules = {
  name: [{ required: true, message: "请输入产品名称", trigger: "blur" }],
  categoryName: [{ required: true, message: "请输入产品类别", trigger: "blur" }],
};
const emit = defineEmits(["close"]);

function close() {
  delete ruleForm.value.id;
    // 重置表单 去除之前的校验
  console.log(ruleFormRef,"ruleForm");
  ruleFormRef.value.resetFields();
  visible.value = false;

  emit("close");
}

// 暴露接口

// 保存用户信息
const saveRep = () => {
  ruleFormRef.value.validate((val) => {
    console.log(val, "val");
    if (val) {
      ruleForm.value.adduserId = getUserInfo().id
      // 更新用户
      if (ruleForm.value.id) {
        console.log("更新仓库");
        console.log(ruleForm.value);
        updateRepository(ruleForm.value).then(resp => {
          ElMessage.success("更新仓库成功！")
          close();
        })
      } else {
        console.log("添加仓库");
        console.log(ruleForm.value);
        saveRepository(ruleForm.value).then(resp => {
          ElMessage.success("添加仓库成功！")
          close();
        })
      }
    }
  });
};
</script>

<!--vue样式 scoped当前样式只适用于当前组件-->
<style scoped>
.ipt {
  width: 90%;
}

.demo-date-picker {
  display: flex;
  width: 100%;
  padding: 0;
  flex-wrap: wrap;
}

.demo-date-picker .block {
  padding: 30px 0;
  text-align: center;
  border-right: solid 1px var(--el-border-color);
  flex: 1;
}

.demo-date-picker .block:last-child {
  border-right: none;
}

.demo-date-picker .demonstration {
  display: block;
  color: var(--el-text-color-secondary);
  font-size: 14px;
  margin-bottom: 20px;
}

.avatar-uploader .avatar {
  width: 178px;
  height: 178px;
  display: block;
}

:deep(.avatar-uploader .el-upload) {
  border: 1px dashed var(--el-border-color);
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  transition: var(--el-transition-duration-fast);
}

.avatar-uploader .el-upload:hover {
  border-color: var(--el-color-primary);
}

.el-icon.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  text-align: center;
}
</style>
